home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Diamond Collection
/
The Diamond Collection (Software Vault)(Digital Impact).ISO
/
cdr34
/
onetime.zip
/
FCODE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-26
|
3KB
|
137 lines
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <io.h>
#define GROUP 5
#define SPACE ' '
#define MAX_INPUT_LEN 30
#define INVOCATION_ERROR 20
#define CMD *argv[1]
#define TRUE 1
#define FALSE 0
#define MAXVAL 256
#define FILE_ERR 11
#define BUFFERSIZE 8192
#define ISCODE(x) x<MAXVAL?TRUE:FALSE
typedef enum { ENCODE, DECODE } Codeflag;
int convert( int ch, Codeflag C );
int code( Codeflag Operation);
void main( int argc, char **argv )
{
char answer[ MAX_INPUT_LEN ];
if( CMD == 'e' || CMD == 'E' )
code( ENCODE );
else
if( CMD == 'd' || CMD == 'D' )
code ( DECODE );
else
{
puts( "Encode or Decode? ");
gets( answer );
if( *answer == 'e' || *answer == 'E' )
code ( ENCODE );
else
if( *answer == 'd' || *answer == 'D' )
code ( DECODE );
else
exit ( INVOCATION_ERROR );
}
}
int code( Codeflag Operation)
{
register unsigned char c;
register int ck;
long flength,
number = 0; //Number of characters operated on.
FILE *plaintxt,
*codekey,
*encfile,
*filename;
if( NULL == ( codekey = fopen( "code.key", "rb" ) ) )
exit ( FILE_ERR );
if( setvbuf( codekey, NULL, _IOFBF, BUFFERSIZE ) )
exit( FILE_ERR );
if( Operation == ENCODE )
{
if( NULL == ( plaintxt = fopen( "plain.txt", "rb" ) ) )
exit( FILE_ERR );
if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
exit( FILE_ERR );
if( NULL == ( encfile = fopen( "encr.xxx", "ab" ) ) )
exit ( FILE_ERR );
if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
exit( FILE_ERR );
}
else
{
if( NULL == ( plaintxt = fopen( "decr.txt", "wb" ) ) )
exit( FILE_ERR );
if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
exit( FILE_ERR );
if( NULL == ( encfile = fopen( "encr.xxx", "rb" ) ) )
exit( FILE_ERR );
if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
exit( FILE_ERR );
}
if( Operation == ENCODE )
filename = plaintxt;
else
filename = encfile;
flength = filelength( fileno( filename ) );
while( number < flength )
{
c = fgetc( filename );
number++;
ck = fgetc( codekey );
if( Operation == ENCODE )
c += ck;
else
c -= ck;
if( Operation == ENCODE )
fputc( c, encfile );
else
fputc( c, plaintxt );
}
fcloseall();
return( number );
}